﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reconciliation.Data
{
    public partial class CaseAttachment
    {
        public static Domain.CaseAttachment MapDataToDomain(Data.CaseAttachment dataCaseAttachment)
        {
            Domain.CaseAttachment domainCaseAttachment = new Reconciliation.Domain.CaseAttachment();
            domainCaseAttachment.Attachment = dataCaseAttachment.Attachment ;
            domainCaseAttachment.CaseAttachmentID= dataCaseAttachment.CaseAttachmentID ;
            domainCaseAttachment.AttachmentName = dataCaseAttachment.AttachmentName;
            dataCaseAttachment.AttachmentType = dataCaseAttachment.AttachmentType;
            domainCaseAttachment.DateTime = dataCaseAttachment.DateTime;
            return domainCaseAttachment;
        }

        public static Data.CaseAttachment MapDomainToData(Domain.CaseAttachment domainCaseAttachment)
        {
            Data.CaseAttachment dataCaseAttachment = new Reconciliation.Data.CaseAttachment();
            dataCaseAttachment.Attachment = domainCaseAttachment.Attachment;
            dataCaseAttachment.CaseAttachmentID = domainCaseAttachment.CaseAttachmentID;
            dataCaseAttachment.DateTime = DateTime.Now;
            dataCaseAttachment.AttachmentName = domainCaseAttachment.AttachmentName;
            dataCaseAttachment.AttachmentType = domainCaseAttachment.AttachmentType;
            dataCaseAttachment.CaseReference.EntityKey = new System.Data.EntityKey("ReconciliationEntities.Case", "CaseID", domainCaseAttachment.Case.CaseID);
            return dataCaseAttachment;
        }

        public static List<Domain.CaseAttachment> GetCaseAttachment(int caseID)
        {
            List<Domain.CaseAttachment> domainCaseAttachmentList = new List<Reconciliation.Domain.CaseAttachment>();
            Domain.CaseAttachment domainCaseAttachment = new Reconciliation.Domain.CaseAttachment();
            ReconciliationEntities recEnt = new ReconciliationEntities();
            List<Data.CaseAttachment> dataCaseAttachment = recEnt.CaseAttachment.Where(c => c.Case.CaseID.Equals(caseID)).ToList();
            foreach (Data.CaseAttachment cac in dataCaseAttachment)
            {
                domainCaseAttachment = CaseAttachment.MapDataToDomain(cac);
                domainCaseAttachmentList.Add(domainCaseAttachment);
            }
            return domainCaseAttachmentList;
        }

        public static void SetAttachment(Domain.CaseAttachment domainAttachment)
        {
            Data.CaseAttachment dataAttachment = Data.CaseAttachment.MapDomainToData(domainAttachment);
            using (ReconciliationEntities recEnt = new ReconciliationEntities())
            {
                recEnt.AddToCaseAttachment(dataAttachment);
                recEnt.SaveChanges();
            }
        }
    }
}
